package in.muton.edushare.pages.publicpages.group;

import in.muton.edushare.EduSharePage;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.FooterLinks;
import in.muton.edushare.panels.ImageHolder;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.GroupsDAO;
import com.edushare.common.dao.GroupsMembersDAO;
import com.edushare.common.util.URLUtils;
import com.edushare.common.vo.GroupMembers;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Universities;

public abstract class GroupBase extends EduSharePage {
	
	protected CayenneModel<Groups> groupModel;
	private Fragment joinDivFrag = null;

	public GroupBase(PageParameters params) {
		int gid = params.getAsInteger("0", 0);
		if(gid==0)
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
		Groups g = new GroupsDAO(getSessionORMInterface()).getGroupById(gid);
		if(g==null)
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
		this.groupModel = new CayenneModel<Groups>(g);
		
		add(CSSPackageResource.getHeaderContribution(GroupBase.class, "structure.css"));
		add(new Label("groupName", new PropertyModel<String>(groupModel, "groupName")));
		add(new Label("pageTitle", new PropertyModel<String>(this, "pageTitle")));
		add(new ImageHolder("img", new PropertyModel<String>(this, "imageUrl")));
		Class<? extends WebPage> univClass = null;
		if(EduShareSession.get().isLoggedIn())
			univClass = URLLoader.getPageClass(URLConstants.UNIVERSITY_PROFILE_PATH);
		else
			univClass = URLLoader.getPageClass(URLConstants.UNIV_PUBLIC_PAGE_HOME);
		
		
		add(new BookmarkablePageLink<Void>("univ", univClass, EntityPageParams.getParams(new CayenneModel<Universities>(groupModel.getObject().getUniversity()))));
		add(new BookmarkablePageLink<Void>("members", URLLoader.getPageClass(URLConstants.GROUPS_PUBLIC_MEMBERS),EntityPageParams.getParams(groupModel)));
		BookmarkablePageLink<Void> home = null;
		if(EduShareSession.get().isLoggedIn())
			home = new BookmarkablePageLink<Void>("home", URLLoader.getPageClass(URLConstants.USER_PROFILE_PATH));
		else{
			home = new BookmarkablePageLink<Void>("home", URLLoader.getPageClass(URLConstants.LOGIN_PATH));
			home.setVisible(false);
		}
		add(home);
		add(new FooterLinks("footerLinks", CSSPackageResource.getHeaderContribution(FooterLinks.class, "FooterWithColorLinks.css")));
	}
	
	public abstract String getPageTitle();
	
	public String getImageUrl(){
		String url = this.groupModel.getObject().getUploadedFileUrl();
		return URLUtils.getFileVersionUrlFromCompleteUrl(url, ApplicationConstants.FILE_SUFFIX_VBADGE);
	}
	
	@Override
	protected void onBeforeRender() {
		//join link
		Fragment f = null;
		if(EduShareSession.get().isLoggedIn()){
			//if a member link to group's home page
			if(EduShareSession.get().getLoggedInUser().isGroupMember(groupModel.getObject())){
				f = new ClickToDisjoinFrag("joinDiv", this, groupModel);
				f.add(new SimpleAttributeModifier("class", "es-bg-link_break16"));
			}else if(EduShareSession.get().getLoggedInUser().getMembershipStatus(groupModel.getObject())== GroupMembers.STATUS_WAITING_APPROVAL){
				f = new PendingApprovalFrag("joinDiv", this);
				f.add(new SimpleAttributeModifier("class", "es-bg-FlagBlue16"));
			}else{
				f = new ClickToJoinFrag("joinDiv", this, groupModel);
				f.add(new SimpleAttributeModifier("class", "es-bg-link16"));
			}
		}else{
			f = new LoginToJoinFrag("joinDiv", this, groupModel);
			f.add(new SimpleAttributeModifier("class", "es-bg-loginLink16"));
		}
		
		if(joinDivFrag!=null){
			joinDivFrag.replaceWith(f);
			joinDivFrag = f;
		}else{
			joinDivFrag = f;
			add(f);
		}
		
		super.onBeforeRender();
	}
	
	//Join div frags
	public class LoginToJoinFrag extends Fragment{

		public LoginToJoinFrag(String id, MarkupContainer markupProvider, IModel<Groups> model) {
			super(id, "loginToJoin", markupProvider, model);
			add(new Link<Void>("loginLink"){

				@Override
				public void onClick() {
					/*if(EduShareSession.get().getLoggedInUser()!=null){
						
					}*/
					throw new RestartResponseAtInterceptPageException(URLLoader.getPageClass(URLConstants.LOGIN_PATH));
				}
				
			});
		}
		
	}
	
	public class ClickToJoinFrag extends Fragment{

		public ClickToJoinFrag(String id, MarkupContainer markupProvider, final IModel<Groups> model) {
			super(id, "join", markupProvider, model);
			add(new Link<Void>("joinLink"){

				@Override
				public void onClick() {
					if(EduShareSession.get().getLoggedInUser()==null)
						return;
					new GroupsMembersDAO(getSessionORMInterface())
							.addNewMember(
									model.getObject(), 
									EduShareSession.get().getLoggedInUser(), 
									GroupMembers.GROUP_MEMBER, 
									GroupMembers.STATUS_WAITING_APPROVAL
									);
					//TODO: Show notification
				}
				
			});
		}
		
	}
	
	public class ClickToDisjoinFrag extends Fragment{

		public ClickToDisjoinFrag(String id, MarkupContainer markupProvider, final IModel<Groups> model) {
			super(id, "disjoin", markupProvider, model);
			add(new Link<Void>("disjoinLink"){

				@Override
				public void onClick() {
					if(EduShareSession.get().getLoggedInUser()==null)
						return;
					for(GroupMembers gm:EduShareSession.get().getLoggedInUser().getGroupMemberships()){
						if(gm.getGroup().getGroupId()==model.getObject().getGroupId()){
							new GroupsMembersDAO(getSessionORMInterface()).remove(gm);
							break;
						}
					}
					//TODO: Show notification
				}
				
			});
		}
		
	}
	
	
	public class PendingApprovalFrag extends Fragment{

		public PendingApprovalFrag(String id, MarkupContainer markupProvider) {
			super(id, "pending", markupProvider);
		}
		
	}
}
